package com.yinNuo.collectWool.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: yinNuo
 * create at: 2022/1/16 下午5:24
 * @description: 自定义定时任务http请求封装
 */
@Component
public class QuartzHttpUtils {

    /**
     * 发送get请求工具类
     *
     * @param url      请求的url链接
     * @param urlParam 请求url链接后携带的参数
     * @param headers  发送请求的请求头
     * @return body
     */
    public String get(String url, String urlParam, String headers) {
        String urlStr = url + urlParam;
        //设置请求头
        Map<String, String> header = new HashMap<>();
        //解析header
        String[] headerList = splitStr(headers, "&");
        for (String str : headerList) {
            String[] strings = splitStr(str, "=");
            String key = "";
            String value = "";
            if (strings.length == 1) {
                key = strings[0];
            } else {
                key = strings[0];
                value = strings[1];
            }
            header.put(key, value);
        }
        return  JSONUtil.parseObj(HttpRequest.get(urlStr).addHeaders(header).execute().body()).toString();
    }

    /**
     * 发送post请求工具类
     *
     * @param url       请求的url链接
     * @param urlParam  请求url链接后携带的参数
     * @param headers   发送请求的请求头
     * @param formParam 表单提交携带的参数
     * @return
     */
    public String post(String url, String urlParam, String headers, String formParam) {
        String urlStr = "";
        if (null == urlParam) {
            urlStr = url;
        } else {
            urlStr = url + urlParam;
        }
        //System.out.println("最后字符串是：" + urlStr);
        //设置请求头
        Map<String, String> header = new HashMap<>();
        //解析header
        String[] headerList = splitStr(headers, "&");
        for (String str : headerList) {
            String[] strings = splitStr(str, "=");
            String key = "";
            String value = "";
            if (strings.length == 1) {
                key = strings[0];
            } else {
                key = strings[0];
                value = strings[1];
            }
            header.put(key, value);
        }
        //设置表单提交的内容
        Map<String, Object> param = new HashMap<>();
        //解析formParam
        String[] formParamList = splitStr(formParam, "&");
        for (String str : formParamList) {
            String[] strings = splitStr(str, "=");
            String key = "";
            String value = "";
            if (strings.length == 1) {
                key = strings[0];
            } else {
                key = strings[0];
                value = strings[1];
            }
            param.put(key, value);
        }
        return JSONUtil.parseObj(HttpRequest.post(urlStr).addHeaders(header).form(param).execute().body()).toString();
    }

    /**
     * 按需分割字符串
     *
     * @param strs      需要分割的字符串
     * @param splitIden 分割标识
     * @return
     */
    public String[] splitStr(String strs, String splitIden) {
        return strs.split(splitIden);
    }
}
